<?php
namespace Shared;

class Page {
    
    protected $rootView;
    protected $view;
    protected $request;
    
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $EntityManager;
    
    public function __construct() {
//        $this->rootView = new \System\Template('@open\index.twig');
        $this->EntityManager = \Shared\Application::getInstance()->getEntityManager();
        $this->request = $this->getRequest();
    }
    
    public function sanitize($str, $type = 'string') {
        switch($type) {
            case 'email':
                $filter = FILTER_SANITIZE_EMAIL;
                break;
            case 'float':
                $filter = FILTER_SANITIZE_NUMBER_FLOAT;
                break;
            case 'int':
                $filter = FILTER_SANITIZE_NUMBER_INT;
                break;
            default:
                $filter = FILTER_SANITIZE_STRING;
                break;
        }
        
        $str = filter_var(trim($str), $filter);
        
        return $str;
    }
    
    public function createNotFound($message = '404 Page not found!') {
        Application::getInstance()->createNotFound($message);
    }
    
    public function getCurrentUser() {
        return Application::getInstance()->getCurrentUser();
    }
    
    public function redirect($url) {
        header('Location: ' . $url, 301);
        exit();
    }
    
    public function generateUrl($name, array $options = array()) {
        $path = \Shared\URLManager::getInstance()->createPath($name, $options);
        return $path;
    }
    
    public function getRequest() {
        $Request = Application::getInstance()->getRequest();
        return $Request;
    }
    
    /**
     * 
     * @return \System\Session
     */
    public function getSession() {
        $Session = Application::getInstance()->getSession();
        return $Session;
    }
    
    public function render() {
//        $this->rootView->content = $this->view ? $this->view->paste() : '';
//        $html = $this->rootView->paste();
        $this->view->impressions = $this->getTodayImpressionsCount();
        $this->view->guests = $this->getTodayGuestsCount();
        $html = $this->view->paste();
        return $html;
    }

    
    public function getTodayGuestsCount() {
        $qb = $this->getBaseQuery($count = true);
        $qb->groupBy('i.ip');
        return count($qb->getQuery()->getResult());
    }
    
    public function getTodayImpressionsCount() {
        $qb = $this->getBaseQuery($count = true);
        
        return $qb->getQuery()->getSingleScalarResult();
    }
    
    public function getBaseQuery($count = false) {
        $qb = $this->EntityManager->createQueryBuilder();
        
        $select = $count ? 'COUNT(i)' : 'i';
        
        $qb
            ->select($select)
            ->from('Shared\Main\Impression', 'i')
            ->where('i.dateTime between ?1 and ?2')
            ->setParameter(1, date('Y-m-d'))
            ->setParameter(2, date('Y-m-d 23:59:59'));
        
        return $qb;
    }
}
